$OpenBSD: patch-taglib_toolkit_tstring_cpp,v 1.2 2014/11/10 10:55:58 dcoppa Exp $

From db3e961d1098d5efe57364f540f68a5996dc83c2 Mon Sep 17 00:00:00 2001
From: Tsuda Kageyu <tsuda.kageyu@gmail.com>
Date: Tue, 13 May 2014 18:22:16 +0900
Subject: [PATCH] Fixed a wrong byte order handling on big-endian machines.

commit 3bf30af66c8fd77a88d9379a0956ddb2fc70dc20
Author: Tsuda Kageyu <tsuda.kageyu@gmail.com>
Date:   Wed Nov 6 17:01:21 2013 +0900

Fix ABI breakage in TagLib::String

--- taglib/toolkit/tstring.cpp.orig	Tue Oct  8 17:50:01 2013
+++ taglib/toolkit/tstring.cpp	Mon Nov 10 10:52:49 2014
@@ -50,7 +50,12 @@ namespace 
 
   inline unsigned short combine(unsigned char c1, unsigned char c2)
   {
-    return (c1 << 8) | c2;
+    using namespace TagLib::Utils;
+
+    if(SystemByteOrder == LittleEndian)
+      return (c1 << 8) | c2;
+    else
+      return (c2 << 8) | c1;
   }
 
   void UTF16toUTF8(const wchar_t *src, size_t srcLength, char *dst, size_t dstLength)
@@ -209,8 +214,16 @@ String::String(const std::string &s, Type t)
 String::String(const wstring &s, Type t)
   : d(new StringPrivate())
 {
-  if(t == UTF16 || t == UTF16BE || t == UTF16LE)
+  if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
+    // This looks ugly but needed for the compatibility with TagLib1.8. 
+    // Should be removed in TabLib2.0.
+    if (t == UTF16BE)
+      t = WCharByteOrder;
+    else if (t == UTF16LE)
+      t = (WCharByteOrder == UTF16LE ? UTF16BE : UTF16LE);
+
     copyFromUTF16(s.c_str(), s.length(), t);
+  }
   else {
     debug("String::String() -- A TagLib::wstring should not contain Latin1 or UTF-8.");
   }
@@ -219,8 +232,16 @@ String::String(const wstring &s, Type t)
 String::String(const wchar_t *s, Type t)
   : d(new StringPrivate())
 {
-  if(t == UTF16 || t == UTF16BE || t == UTF16LE)
+  if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
+    // This looks ugly but needed for the compatibility with TagLib1.8. 
+    // Should be removed in TabLib2.0.
+    if (t == UTF16BE)
+      t = WCharByteOrder;
+    else if (t == UTF16LE)
+      t = (WCharByteOrder == UTF16LE ? UTF16BE : UTF16LE);
+
     copyFromUTF16(s, ::wcslen(s), t);
+  }
   else {
     debug("String::String() -- A const wchar_t * should not contain Latin1 or UTF-8.");
   }
